package com.sgb.kjwl.view.ui.activity.self.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMonitoringReportBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.swgk.core.view.entity.ChannelEntity;
import com.sgb.kjwl.view.ui.adapter.home.HomeFragmentPagerAdapter;
import com.sgb.kjwl.view.ui.fragment.self.mine.CommonFocusFragment;
import com.sgb.kjwl.viewmodel.self.mine.MonitoringReportModel;
import com.swgk.core.base.BaseFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * 举报监督主页 2021/03/05  @chenT
 */
public class MonitoringReportActivity extends AppActivity {
    private ActivityMonitoringReportBinding binding;

    @Inject
    MonitoringReportModel viewModel;

    private float unSelectedSize; //默认标题字体大小
    private float selectedSize; //选中时标题栏目字体大小
    private boolean isSelect;//选中

    private List<ChannelEntity> channelEntityList;
    private String[] title = {"收到的举报", "发起的举报"};

    private HomeFragmentPagerAdapter goodsFragmentPagerAdapter;
    private int selectPosition = 0;
    private List<BaseFragment> chooseFragments;
    private TextView selectTextView;
    private String tabSelectName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(MonitoringReportActivity.this, R.layout.activity_monitoring_report);
        selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18, getResources().getDisplayMetrics());
        unSelectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15, getResources().getDisplayMetrics());
        chooseFragments = new ArrayList<>();
        channelEntityList = new ArrayList<>();
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MonitoringReportActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("举报监督");
        getDataList();
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                selectPosition = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        binding.tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.getPaint().setFakeBoldText(true);
                tv.setTextColor(ContextCompat.getColor(MonitoringReportActivity.this, R.color.color_FFFFFF));
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tv.setPadding(0, 0, 0, 10);
                tv.invalidate();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                tv.setPadding(0, 0, 0, 10);
                tv.getPaint().setFakeBoldText(false);
                tv.setTextColor(ContextCompat.getColor(MonitoringReportActivity.this, R.color.color_C0C4CC));
                tv.invalidate();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void getDataList() {
        channelEntityList.clear();
        chooseFragments.clear();
        for (int i = 0; i < title.length; i++) {
            ChannelEntity channelEntity = new ChannelEntity();
            channelEntity.setName(title[i]);
            channelEntity.setId(i);
            channelEntityList.add(channelEntity);
            chooseFragments.add(CommonFocusFragment.newInstance(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""), i));
        }
        goodsFragmentPagerAdapter = new HomeFragmentPagerAdapter(getSupportFragmentManager(), chooseFragments, channelEntityList);
        binding.viewpager.setAdapter(goodsFragmentPagerAdapter);
        binding.tablayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(0);
        for (int i = 0; i < title.length; i++) {
            binding.tablayout.getTabAt(i).setCustomView(R.layout.item_tablayout_view_1);
            TextView tv = binding.tablayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item);
            tv.setText(title[i]);
            if (i == 0) {
                tv.setTextColor(ContextCompat.getColor(MonitoringReportActivity.this, R.color.color_FFFFFF));
                tv.getPaint().setFakeBoldText(true);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tv.setPadding(0, 0, 0, 10);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}